package text;

import cn.hutool.json.JSONUtil;
import com.hmall.api.domain.po.Item;
import com.hmall.api.domain.po.ItemDoc;
import com.hmall.item.controller.SearchController;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.naming.directory.SearchResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class EsIndex1Text {
    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.202.135:9200")
        ));
    }

    @Test
    void testConnect() {
        System.out.println("client: " +client);
    }

    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
    @Test
    void  text() throws IOException {
        SearchRequest request=new SearchRequest("items");
        request.source().size(0);
        String name="open";
        request.source().aggregation(AggregationBuilders.terms(name).field("category.keyword")).stats();
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//        System.out.println(search);
        Aggregations aggregations = response.getAggregations();
        Terms aggregation = aggregations.get(name);
        List<? extends Terms.Bucket> buckets = aggregation.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            System.out.println("ket: " + bucket.getKeyAsString());
            System.out.println("number: "+bucket.getDocCount());
        }
    }

    List<ItemDoc> hub(SearchResponse search){
        SearchHits hits = search.getHits();
        long number= hits.getTotalHits().value;
        SearchHit[] hits1 = hits.getHits();
        List<ItemDoc>itemDocs=new ArrayList<>();
        for (SearchHit documentFields : hits1) {
            itemDocs.add(JSONUtil.toBean(documentFields.getSourceAsString(), ItemDoc.class));
        }
        return itemDocs;
    }
    @Test
    void  text2() throws IOException {
        SearchRequest searchRequest=new SearchRequest("items");
//        searchRequest.source().query(QueryBuilders.matchAllQuery()).size(1000);
//        searchRequest.source().query(QueryBuilders.matchQuery("name", "牛奶"));
//        searchRequest.source().from(6);
//        searchRequest.source().size(7);
//        searchRequest.source().sort("sold", SortOrder.DESC);
//        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
//        boolQueryBuilder.filter(QueryBuilders.termQuery("brand.keyword", "蒙牛"));
//        searchRequest.source().query(boolQueryBuilder);
//        searchRequest.source().query(QueryBuilders.rangeQuery("price").gte(2000));
//        searchRequest.source().query(QueryBuilders.rangeQuery("price").lte(1000));
//        searchRequest.source().aggregation(AggregationBuilders.terms("category").field("category.keyword").size(20));
//        searchRequest.source().aggregation(
//                AggregationBuilders.terms("brand").field("brand.keyword").size(20)
//        );
//        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
//        Aggregations aggregations = search.getAggregations();
//
//        Terms Categoryaggregation = aggregations.get("category");
//        List<? extends Terms.Bucket> Categorybuckets = Categoryaggregation.getBuckets();
//        List<String>categorys=new ArrayList<>();
//
//        for (Terms.Bucket bucket : Categorybuckets) {
//            categorys.add(bucket.getKey().toString());
//        }
//        Terms Brandaggregation = aggregations.get("brand");
//        List<? extends Terms.Bucket> Brandbuckets = Brandaggregation.getBuckets();
//        List<String>brands=new ArrayList<>();
//
//        for (Terms.Bucket bucket : Brandbuckets) {
//            brands.add(bucket.getKey().toString());
//        }
//        Map<String,String>map=new HashMap<>();
//        map.put("category",categorys.toString());
//        map.put("brand",brands.toString());
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("price").gt("30000").lt("31100"));
        boolQueryBuilder.filter(QueryBuilders.termQuery("brand.keyword", "小米"));
        searchRequest.source().query(boolQueryBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(search);
    }
}
